package cn.yunhe.service.impl;

import cn.yunhe.dao.PersonMapper;
import cn.yunhe.entity.Page;
import cn.yunhe.entity.Person;
import cn.yunhe.service.IPersonService;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class PersonService implements IPersonService {

    @Autowired
    @Getter
    @Setter
    private PersonMapper personMapper;

    @Resource
    private Page page;

    @Override
    public Page queryLikePerson(Map<String, Object> cond) {
        //根据前台条件查询记录总条数
        page.setTotalNum(personMapper.getLikeUsersCount(cond));

        //从前台数据中获得每页的大小
        int pageSize = Integer.parseInt(String.valueOf(cond.get("pageSize")));
        page.setPageSize(pageSize);
        //从前台数据中获得当前页码
        int pageNum = Integer.parseInt(String.valueOf(cond.get("pageNum")));
        page.setCurPageNum(pageNum);

        //计算总页数
        page.setTotalPage(page.getTotalNum() / page.getPageSize() + (page.getTotalNum() % page.getPageSize() == 0 ? 0 : 1));

        //根据条件获得结果列表,条件判断后的查询结果
        List<Object> personList =personMapper.getLikePerson(cond);

        page.setResult(personList);

        return page;
    }

    @Override
    public void addPerson(Person person) {
        personMapper.addPerson(person);
    }

    @Override
    public void delPerson(Person person) {
        personMapper.delPerson(person);
    }

    @Override
    public Person getPersonById(Person person) {
        Person person1=personMapper.getPersonById(person);
        return person1;
    }

    @Override
    public void updatePerson(Person person) {
        personMapper.updatePerson(person);
        System.out.println(person.getName());
    }


}
